﻿@model DirectMessagesModel

@using System.Globalization
@using CkSoftware.GroupMe.Sdk.Core.Dao
@using CkSoftware.GroupMe.Sdk.Helpers.Emoji
@using CkSoftware.GroupMe.Sdk.TestWeb.Helpers
@using CkSoftware.GroupMe.Sdk.TestWeb.Models
@using TwitterBootstrapMVC.Controls

@{
    ViewBag.Title = "Direct messages to '" + Model.Member.Nickname + "'";
}

<div class="page-header">
    <h1>Direct messages to '@Model.Member.Nickname'</h1>
</div>

@if (!SessionConfiguration.IsConfigValueIsSet<string>(SessionConfiguration.GroupMeClientAuthKey))
{
    <h2>Configuration required</h2>
    <p>Before you can start here, you have to configure your GroupMe Access Token @Html.Bootstrap().ActionLink("here", "Index").</p>
}
else
{
    <h2>Add new message</h2>
    
    using (FormBuilder<DirectMessagesModel> form = Html.Bootstrap().Begin(new Form("CreateDirectMessage").Type(FormType.Horizontal)))
    {
        @Html.HiddenFor(m => m.Member.UserId)
        @Html.HiddenFor(m => m.Group.Id)
	    @form.ControlGroup().TextBoxFor(m => m.NewMessageText).HelpText("You also can use emoji here. Use format '{{PACKID,EMOJIID}}', example: {{1,0}}.").Label().LabelText("Text")
	    @form.ControlGroup().TextBoxFor(m => m.NewMessageImageUrl).Label().LabelText("Image-Attachment url")
        @form.ControlGroup().CustomControls(Html.Bootstrap().SubmitButton())
    }
    
    <h2>Messages @Html.Bootstrap().ActionLinkButton(string.Empty, "RefreshDirectMessages", "Groups").Class("pull-right").AppendIcon(Icons.refresh).Tooltip(new Tooltip("Refreshs all messages").Placement("top")).RouteValues(new { memberId = Model.Member.UserId, groupId = Model.Group.Id })</h2>
    
    <p class="alert alert-info">
        The messages below are cached, so if there are any changes (for example likes), the likes count will not be updated until the 'refresh' button on the right is clicked.
    </p>
    
    using(var messagesTable = Html.Bootstrap().Begin(new Table().Striped()))
    {
        using (messagesTable.BeginHeader())
        {
            <tr>
                <th>Avatar</th>
                <th>Message ID</th>
                <th>IsSystemMessage</th>
                <th>Created At</th>
                <th>Nickname</th>
                <th>Text</th>
                <th>Likes</th>
                <th>Actions</th>
            </tr>
        }
        using (var messageTableBody = messagesTable.BeginBody())
        {
            foreach (var message in Model.Messages)
            {
                using (var messageRow = messageTableBody.BeginRow())
                {
                    @messageRow.Cell(Html.AvatarImageForGroupMePicture(message.Avatar, message.Name))
                    @messageRow.Cell(message.MessageId)
                    @messageRow.Cell(message.IsSystemMessage.ToString())
                    @messageRow.Cell(message.CreatedAt.ToShortDateString() + " " + message.CreatedAt.ToShortTimeString())
                    @messageRow.Cell(message.Name)
                    @messageRow.Cell(GroupMeEmojiMessageRenderer.MergeMessageWithEmojiImages(message.Text, (GroupMeEmojiAttachment)message.Attachments.FirstOrDefault(at => at.Type == AttachmentType.Emoji)))
                    @messageRow.Cell(message.LikedByUserIds.Count().ToString(CultureInfo.CurrentCulture))
                    using (messageRow.BeginCell())
                    {
                        using (var buttonGroup = Html.Bootstrap().Begin(new ButtonGroup()))
                        {
                            @buttonGroup.ActionLinkButton(string.Empty, "LikeDirectMessage", "Groups").AppendIcon(Icons.heart).Tooltip(new Tooltip("Likes this message").Placement("top")).RouteValues(new { memberId = Model.Member.UserId, groupId = Model.Group.Id, messageId = message.MessageId })
                            @buttonGroup.ActionLinkButton(string.Empty, "UnlikeDirectMessage", "Groups").AppendIcon(Icons.thumbs_down).Tooltip(new Tooltip("Unlikes this message").Placement("top")).RouteValues(new { memberId = Model.Member.UserId, groupId = Model.Group.Id, messageId = message.MessageId })
                        }
                    }
                }
            }
        }
    }
    
    @Html.ActionLink("Load more messages", "OlderDirectMessages", new { memberId = Model.Member.UserId, groupId = Model.Group.Id })
}